package com.simplehire.controller;

import com.simplehire.entity.ResumeMatchResult;
import com.simplehire.model.InterviewResult;
import com.simplehire.model.User;
import com.simplehire.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 报告导出控制器 - MVP V4
 * 核心功能：报告导出和系统集成
 */
@Controller
@RequestMapping("/reports")
public class ReportController {
    
    @Autowired
    private ReportService reportService;
    
    /**
     * 显示报告页面
     */
    @GetMapping
    public String showReportsPage(Model model, Authentication authentication) {
        User currentUser = (User) authentication.getPrincipal();
        
        // 获取用户的面试结果和匹配结果
        List<InterviewResult> interviewResults = reportService.getUserInterviewResults(currentUser.getId());
        List<ResumeMatchResult> matchResults = reportService.getUserMatchResults(currentUser.getId());
        
        model.addAttribute("interviewResults", interviewResults);
        model.addAttribute("matchResults", matchResults);
        model.addAttribute("user", currentUser);
        
        return "reports";
    }
    
    /**
     * 导出面试报告为PDF
     */
    @GetMapping("/interview/{id}/pdf")
    public ResponseEntity<ByteArrayResource> exportInterviewReportPdf(
            @PathVariable Long id, Authentication authentication) {
        
        User currentUser = (User) authentication.getPrincipal();
        byte[] pdfData = reportService.generateInterviewReportPdf(id, currentUser.getId());
        
        String filename = "interview_report_" + id + "_" + 
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".pdf";
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                .contentType(MediaType.APPLICATION_PDF)
                .contentLength(pdfData.length)
                .body(new ByteArrayResource(pdfData));
    }
    
    /**
     * 导出岗位匹配报告为PDF
     */
    @GetMapping("/match/{id}/pdf")
    public ResponseEntity<ByteArrayResource> exportMatchReportPdf(
            @PathVariable Long id, Authentication authentication) {
        
        User currentUser = (User) authentication.getPrincipal();
        byte[] pdfData = reportService.generateMatchReportPdf(id, currentUser.getId());
        
        String filename = "match_report_" + id + "_" + 
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".pdf";
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                .contentType(MediaType.APPLICATION_PDF)
                .contentLength(pdfData.length)
                .body(new ByteArrayResource(pdfData));
    }
    
    /**
     * 导出综合报告为Excel
     */
    @GetMapping("/comprehensive/excel")
    public ResponseEntity<ByteArrayResource> exportComprehensiveReportExcel(
            Authentication authentication) {
        
        User currentUser = (User) authentication.getPrincipal();
        byte[] excelData = reportService.generateComprehensiveReportExcel(currentUser.getId());
        
        String filename = "comprehensive_report_" + currentUser.getUsername() + "_" + 
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".xlsx";
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .contentLength(excelData.length)
                .body(new ByteArrayResource(excelData));
    }
    
    /**
     * 获取报告统计数据API
     */
    @GetMapping("/api/stats")
    @ResponseBody
    public ResponseEntity<?> getReportStats(Authentication authentication) {
        User currentUser = (User) authentication.getPrincipal();
        return ResponseEntity.ok(reportService.getReportStats(currentUser.getId()));
    }
}